package com.moi.summer;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;


public class CharacterAdapter extends ArrayAdapter<Character> {
    private int resourceId;
    public CharacterAdapter (Context context, int textViewResourceId, List<Character> objects) {
        super(context, textViewResourceId, objects);
        resourceId = textViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Character character = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId,null);
            viewHolder = new ViewHolder();
            viewHolder.characterName = (TextView) view.findViewById(R.id.character_name);
            viewHolder.characterSex = (TextView) view.findViewById(R.id.character_sex);
            view.setTag(viewHolder);
        } else  {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.characterName.setText(character.getName());
        viewHolder.characterSex.setText(character.getSex());
        return view;
    }
    class ViewHolder {
        TextView characterName;
        TextView characterSex;
    }
}
